﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using Microsoft.WindowsAPICodePack.Taskbar;
using System.IO;



namespace Emessenger
{
    /// <summary>
    /// Interaction logic for Chat.xaml
    /// </summary>
    public partial class Chat : Window
    {
        string exchangeFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\Dossier E-Messenger Exchange";
        Boolean SendFile = false;
        private string JID;
        //JID = "admin@elmagnifico"; //Test statique 
        private WODXMPPCOMLib.wodXMPPComClass wodXMPP1;

        //passage de l'instance de la connection a cette fenetre "pour ne pas avoir plusieur conexion a la fois"
        public Chat(string jid,WODXMPPCOMLib.wodXMPPComClass wodXMPP)
        {
            InitializeComponent();
            
            wodXMPP1 = wodXMPP;
            wodXMPP1.IncomingMessage += new WODXMPPCOMLib._IwodXMPPComEvents_IncomingMessageEventHandler(wodXMPP1_IncomingMessage);


            
            wodXMPP1.FileTransferStart += new WODXMPPCOMLib._IwodXMPPComEvents_FileTransferStartEventHandler(wodXMPP1_FileTransferStart);
            wodXMPP1.FileTransferEnd += new WODXMPPCOMLib._IwodXMPPComEvents_FileTransferEndEventHandler(wodXMPP1_FileTransferEnd);
            
            //etat(WODXMPPCOMLib);
            wodXMPP1.ContactStatusChange+=new WODXMPPCOMLib._IwodXMPPComEvents_ContactStatusChangeEventHandler(wodXMPP1_ContactStatusChange);

            wodXMPP1.VCardDetails+=new WODXMPPCOMLib._IwodXMPPComEvents_VCardDetailsEventHandler(wodXMPP1_VCardDetails);
               
            
        }
//////////////////////////////////////////////////////////
        
        void wodXMPP1_VCardDetails(WODXMPPCOMLib.XMPPContact Contact, bool Partial)
        {
            MessageBox.Show(Contact.Nick);
        
        }


        
        /// <summary>
        /// Afficher un message quand le correspandant c'est déconnecter
        /// </summary>
        /// <param name="Contact"></param>
        /// <param name="ChatRoom"></param>
        /// <param name="NewStatus"></param>
        /// <param name="OldStatus"></param>
        void wodXMPP1_ContactStatusChange(WODXMPPCOMLib.XMPPContact Contact, WODXMPPCOMLib.XMPPChatRoom ChatRoom, WODXMPPCOMLib.StatusEnum NewStatus, WODXMPPCOMLib.StatusEnum OldStatus)
        {
            if (Contact.Status == WODXMPPCOMLib.StatusEnum.Offline)
                
                textBox3.Text += ("\n"+Contact.Name + " déconnecté à " + DateTime.UtcNow.ToLongTimeString()+"\n");
        }

        /// <summary>
        /// Reception du message text
        /// </summary>
        /// <param name="Contact"></param>
        /// <param name="ChatRoom"></param>
        /// <param name="Message"></param>
        void wodXMPP1_IncomingMessage(WODXMPPCOMLib.XMPPContact Contact, WODXMPPCOMLib.XMPPChatRoom ChatRoom, WODXMPPCOMLib.XMPPMessage Message)
        { 
            textBox3.Text += Contact.Nick + ": " + Message.Text + "\r\n";
            textBox3.ScrollToEnd();
        }


        

        private void entrer(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            // envoyer le message si l'utilisateur tape la boutton "Entrer"
            {
                JID = textBox2.Text;

                wodXMPP1.SendText(JID, textBox1.Text);
                textBox3.Text += "Me: " + textBox1.Text + "\n";
                textBox1.Text = "";

                textBox3.ScrollToEnd();
            }
        }

        
        // Mais quel galaire de 15h.30 à 00h pour réaliser ça !!!
        // Cette méthode permet d'envoyer un fichier à un utilisateur 


        private void FileSend_Click(object sender, RoutedEventArgs e)
        {
            // méthode OpenFileDialog n'existe pas dans WPF d'ou il faut utiliser l'antienne méthode les Forms
            // Il faux specifier explecittement la bibliothèque à utiliser 

            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Multiselect = false;

            // Filtrer le type de fichiers
            // ofd.Filter = "Data Sources (*.txt, *.xml)|*.txt*;*.xml|All Files|*.*";

            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string[] filePath = ofd.FileNames;
                string[] safeFilePath = ofd.SafeFileNames;
                wodXMPP1.SendFile(textBox2.Text,ofd.FileName, "");
                SendFile = true;
            }
            //SendFile = false;
        }


        private void drop_send(object sender, DragEventArgs e)
        {
            string[] fileNames = e.Data.GetData(DataFormats.FileDrop, true) as string[];

            if (fileNames.Length > 0)
            { 
                wodXMPP1.SendFile(textBox2.Text, fileNames[0], "");
                SendFile = true;
            }

            // Mark the event as handled, so the control's native Drop handler is not called.
            e.Handled = true;
        }

        /*
        void wodXMPP1_FileTransferStart(WODXMPPCOMLib.XMPPContact Contact, WODXMPPCOMLib.XMPPFile File, ref WODXMPPCOMLib.XMPPActionsEnum Action)
        {
            if (SendFile == true)
            {
                
                Action = WODXMPPCOMLib.XMPPActionsEnum.Allow;
                SendFile = false;
                // supprimer les cordoneés de fichier envoyer
                //wodXMPP1.SendFile(null, null, null);
            }
            else
                textBox3.Text += "\n{ Vous avez annulé la transfert du fichier.}\n";
        }*/



        void wodXMPP1_FileTransferStart(WODXMPPCOMLib.XMPPContact Contact, WODXMPPCOMLib.XMPPFile File, ref WODXMPPCOMLib.XMPPActionsEnum Action)
        {

            //MessageBox.Show(exchangeFolder );
            if (SendFile == true)
            {
                Action = WODXMPPCOMLib.XMPPActionsEnum.Allow;
                SendFile = false;
            }
            else
            {

                //soundNotification.Play(); // alerte sonnore :p
                if (MessageBox.Show("Contact: "
                    + Contact.JID
                    + "\r\nNom du fichier: "
                    + File.Name + "\r\nTaille du fichier: "
                    + File.Size + " bytes", Contact.Nick
                    + " vous envoie un fichier.", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    //System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
                    // 
                    //sfd.InitialDirectory = exchangeFolder;
                    //sfd.FileName = File.Name;
                    //if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)


                    //créer le dossier si il n'existe pas 
                    /*
                    if (Directory.Exists(exchangeFolder)==false) 
                    {
                        System.Diagnostics.Process.Start(exchangeFolder);
                    }*/

                    if (Directory.Exists(exchangeFolder) == false)
                    {
                        Directory.CreateDirectory(exchangeFolder);
                    }

                    Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();

                    sfd.InitialDirectory = exchangeFolder;
                    sfd.FileName = File.Name;

                    if (sfd.ShowDialog() == true)
                    {
                        Action = WODXMPPCOMLib.XMPPActionsEnum.Allow;
                    }
                    else
                    {
                        Action = WODXMPPCOMLib.XMPPActionsEnum.Deny;
                    }

                }
            }
        }






        void wodXMPP1_FileTransferEnd(WODXMPPCOMLib.XMPPContact Contact, WODXMPPCOMLib.XMPPFile File, int ErrorCode, string ErrorText)
        {
            if (ErrorCode != 0)
            {
                textBox3.Text += ("FileTransfer Error: " + ErrorText);
                SendFile = false;
            }
            else
                textBox3.Text += "Fichier envoyer\n";
            SendFile = false;
        }







        private void deplasser(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
        	this.DragMove();
        }

        

        

        
    // FIN //
    }
}
